<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onAfterViewTicket', 'plgHelpDeskFrequents_onAfterViewTicket' );
$_MAMBOTS->registerFunction( 'onAfterEditTicket', 'plgHelpDeskFrequents_onAfterEditTicket' );

	/**
	 * Do the plugin
	 * @return 
	 */
	function plgHelpDeskFrequents_onAfterViewTicket( $row, $user ) {
		$return = plgHelpDeskFrequents::onAfterViewTicket( $row, $user );
		return $return;
	}
	
	/**
	 * Do the plugin
	 * @return 
	 */
	function plgHelpDeskFrequents_onAfterEditTicket( $row, $user ) {
		$return = plgHelpDeskFrequents::onAfterEditTicket( $row, $user );
		return $return;
	}

	class plgHelpDeskFrequents 
	{
		/**
		 * Gets a parameter value
		 *
		 * @access public
		 * @return mixed Parameter value
		 * @since 1.5
		 */
		function _getParameter( $name, $default=null ) 
		{
			static $params;
			
			if (!is_object($params))
			{
				$database = HelpDeskConfig::getDBO();
				$folder = strtolower( 'HelpDesk' );
				$element = strtolower( 'frequents' );
				
				$return = "";
		
				$query = "
					SELECT 
						`params`
					FROM 
						#__mambots
					WHERE 
						`element` = '{$element}'
					AND 
						`folder` = '{$folder}'
				";
				$database->setQuery( $query );
				$mambot = $database->loadObject();
				
				$params = new mosParameters( $mambot->params );			
			}
			
			$return = $params->get( $name, $default );
			return $return;
		}
		
		/**
		 * Gets records
		 *
		 * @param 	array		holds the old user data
		 * @param 	boolean		true if a new user is stored
		 */	
		function getData() 
		{
			static $_data;
			
			if (!is_array($_data)) 
			{
				$database = HelpDeskConfig::getDBO();
				
				$published_query = " AND `published` = '1' ";
				$display_unpublished = plgHelpDeskFrequents::_getParameter('display_unpublished', '0');
				if (intval($display_unpublished == '1')) 
				{
					$published_query = "";
				}
				
				$query = "
					SELECT 
						* 
					FROM 
						#__helpdesk_frequents
					WHERE 1
						{$published_query}
					ORDER BY 
						`ordering` ASC 
				";
				$database->setQuery( $query );
				$_data = $database->loadObjectList();
			}
	
			return $_data;
		}
	
		/**
		 * @param array Data
		 * @return HTML
		 */
		function renderTabs( $data ) 
		{
			$html = '';
			
			if (!is_array($data)) {
				$data = plgHelpDeskFrequents::getData();
			}
			//instantiate new tabs pane
			$pane = HelpDeskPane::getInstance('tabs', array('startOffset'=>0)); 
			$html .= $pane->startPane( 'pane' );
			// process data, add new tab for each frequent
			for ($i=0; $i<count($data); $i++) {
				$d = $data[$i];	
				//might need to echo the pane commands		
				$html .= $pane->startPanel( $d->title, 'panel'& $i );
				$html .= nl2br( $d->description );			
				$html .= $pane->endPanel();				
			}
			$html .= $pane->endPane();
			return $html;
		}
	
		/**
		 * 
		 * @return HTML
		 */
		function renderHtml() 
		{
			$html = '';
			
			if (!$data = plgHelpDeskFrequents::getData()) 
			{
				return $html;
			}
	
			// display title
			$title = HelpDeskText::_( 'Frequently Used Text' );
			
			$html .= '<p>
					<table class="userlist">
						<thead>
							<tr>
								<th class="title">'.
								$title
								.'</th>
							</tr>
						</thead>
					  </table>
					  </p>
					  ';
	
			// process data
			$html .= plgHelpDeskFrequents::renderTabs( $data );
					
			return $html;
		}
	
		/**
		 *
		 * Method is called after displaying HelpDesk
		 *
		 */
		function onAfterViewTicket( $row, $user ) 
		{
			$mainframe 	= HelpDeskConfig::getApplication();
			$success = false;
			$html = '';
	
			if ($mainframe->isAdmin()) {
	
				// if we're viewing a ticket, set html = the frequents (in tabs)
				$html = plgHelpDeskFrequents::renderHtml();
				
			}
			
			echo $html;
			
			return $success;	
		}
	
		/**
		 *
		 * Method is called after displaying form
		 *
		 */
		function onAfterEditTicket( $row, $user ) 
		{
			$mainframe 	= HelpDeskConfig::getApplication();
			$success = false;
			$html = '';
			
			if ($mainframe->isAdmin()) {
				// if we're viewing a ticket, set html = the frequents (in tabs)
				$html = plgHelpDeskFrequents::renderHtml();
				
			}
			
			echo $html;
			
			return $success;
			
		}	
	}